/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reyes.mysite.struts;

import java.util.List;
import java.text.MessageFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import reyes.mysite.jdo.Blog;
import reyes.mysite.dao.GenericDAO;

/**
 *
 * @author reyesyang
 */
public class QueryBlogAction extends org.apache.struts.action.Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        request.setCharacterEncoding("utf-8");
        int blogCountPerPage = 7;
        String scope = request.getParameter("scope");

        int page = Integer.valueOf(request.getParameter("page"));
        String condition = request.getParameter("condition");
        int blogStart = (page - 1) * blogCountPerPage;
        int blogEnd = page * blogCountPerPage;

        String queryString = null;
        String queryAllBlog = null;

        if (scope.equals("all")) {
            queryAllBlog = "select from " + Blog.class.getName();
            queryString = MessageFormat.format(
                    "select from {0} order by date desc range {1},{2}",
                    Blog.class.getName(),
                    blogStart,
                    blogEnd);
        } else if (scope.equals("category")) {
            queryAllBlog = queryString = MessageFormat.format(
                    "select from {0} where category=={1}",
                    Blog.class.getName(),
                    "'" + condition + "'");
            queryString = MessageFormat.format(
                    "select from {0} where category=={1} order by date desc range {2},{3}",
                    Blog.class.getName(),
                    "'" + condition + "'",
                    blogStart,
                    blogEnd);
        } else if (scope.equals("privacy")) {
            queryAllBlog = MessageFormat.format(
                    "select from {0} where privacy=={1}",
                    Blog.class.getName(),
                    "'" + condition + "'");
            queryString = MessageFormat.format(
                    "select from {0} where privacy=={1} order by date desc range {2},{3}",
                    Blog.class.getName(),
                    "'" + condition + "'",
                    blogStart,
                    blogEnd);
        } else if (scope.equals("search")) {
            condition = condition.toLowerCase();
            queryAllBlog = MessageFormat.format(
                    //"select from {0} where nickTitle.matches({1})",
                    "select from {0} where nickTitle.matches({1})",
                    Blog.class.getName(),
                    "'" + condition + ".*'");
            queryString = MessageFormat.format(
                    //"select from {0} where nickTitle.matches({1}) range {2},{3}",
                    "select from {0} where nickTitle.matches({1}) range {2},{3}",
                    Blog.class.getName(),
                    "'" + condition + ".*'",
                    blogStart,
                    blogEnd);
        }
        List<Blog> blogs = null;
        GenericDAO<Blog> blogDAO = new GenericDAO<Blog>();


        try {
            blogs = blogDAO.query(queryAllBlog);
            int blogCount = blogs.size();
            int pageCount = blogCount / blogCountPerPage;
            if ((blogCount % blogCountPerPage) != 0) {
                pageCount++;
            }

            blogs = blogDAO.query(queryString);
            blogs.size();

            request.setAttribute("blogs", blogs);
            request.setAttribute("scope", scope);
            request.setAttribute("condition", condition);
            request.setAttribute("pageCount", pageCount);
            request.setAttribute("page", page);
        } catch (Exception e) {
            System.out.println("Exception from qeury blog! Details: " + e.getMessage());
        } finally {
            blogDAO.closePM();
        }

        String destPage = null;
        UserService userService = UserServiceFactory.getUserService();
        if (userService.isUserLoggedIn()) {
            if (userService.isUserAdmin()) {
                destPage = "adminBlog";
            } else {
                destPage = "blog";
            }
        } else {
            destPage = "blog";
        }

        return mapping.findForward(destPage);
    }
}
